home *** CD-ROM | disk | FTP | other *** search
/ OpenGL Superbible (2nd Edition) / OpenGL SuperBible e2.iso / tools / Mesa-3.0 / SRC / DEPTH.C < prev    next >
Encoding:
C/C++ Source or Header  |  1998-08-24  |  18.5 KB  |  881 lines

  1. /* $Id: depth.c,v 3.4 1998/08/23 22:17:42 brianp Exp $ */
  2.  
  3. /*
  4.  * Mesa 3-D graphics library
  5.  * Version:  3.0
  6.  * Copyright (C) 1995-1998  Brian Paul
  7.  *
  8.  * This library is free software; you can redistribute it and/or
  9.  * modify it under the terms of the GNU Library General Public
  10.  * License as published by the Free Software Foundation; either
  11.  * version 2 of the License, or (at your option) any later version.
  12.  *
  13.  * This library is distributed in the hope that it will be useful,
  14.  * but WITHOUT ANY WARRANTY; without even the implied warranty of
  15.  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
  16.  * Library General Public License for more details.
  17.  *
  18.  * You should have received a copy of the GNU Library General Public
  19.  * License along with this library; if not, write to the Free
  20.  * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21.  */
  22.  
  23.  
  24. /*
  25.  * $Log: depth.c,v $
  26.  * Revision 3.4  1998/08/23 22:17:42  brianp
  27.  * moved gl_DepthRange() to matrix.c
  28.  *
  29.  * Revision 3.3  1998/06/01 00:35:13  brianp
  30.  * don't clear depth buffer if depth buffer write mask is false
  31.  *
  32.  * Revision 3.2  1998/03/27 03:39:14  brianp
  33.  * fixed G++ warnings
  34.  *
  35.  * Revision 3.1  1998/02/08 20:18:41  brianp
  36.  * removed unneeded headers
  37.  *
  38.  * Revision 3.0  1998/01/31 20:50:23  brianp
  39.  * initial rev
  40.  *
  41.  */
  42.  
  43.  
  44. /*
  45.  * Depth buffer functions
  46.  */
  47.  
  48.  
  49. #ifdef PC_HEADER
  50. #include "all.h"
  51. #else
  52. #include <stdlib.h>
  53. #include <string.h>
  54. #include "context.h"
  55. #include "depth.h"
  56. #include "macros.h"
  57. #include "types.h"
  58. #endif
  59.  
  60.  
  61.  
  62. /**********************************************************************/
  63. /*****                          API Functions                     *****/
  64. /**********************************************************************/
  65.  
  66.  
  67.  
  68. void gl_ClearDepth( GLcontext* ctx, GLclampd depth )
  69. {
  70.    if (INSIDE_BEGIN_END(ctx)) {
  71.       gl_error( ctx, GL_INVALID_OPERATION, "glClearDepth" );
  72.       return;
  73.    }
  74.    ctx->Depth.Clear = (GLfloat) CLAMP( depth, 0.0, 1.0 );
  75. }
  76.  
  77.  
  78.  
  79. void gl_DepthFunc( GLcontext* ctx, GLenum func )
  80. {
  81.    if (INSIDE_BEGIN_END(ctx)) {
  82.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthFunc" );
  83.       return;
  84.    }
  85.  
  86.    switch (func) {
  87.       case GL_NEVER:
  88.       case GL_LESS:    /* (default) pass if incoming z < stored z */
  89.       case GL_GEQUAL:
  90.       case GL_LEQUAL:
  91.       case GL_GREATER:
  92.       case GL_NOTEQUAL:
  93.       case GL_EQUAL:
  94.       case GL_ALWAYS:
  95.          ctx->Depth.Func = func;
  96.          ctx->NewState |= NEW_RASTER_OPS;
  97.          break;
  98.       default:
  99.          gl_error( ctx, GL_INVALID_ENUM, "glDepth.Func" );
  100.    }
  101. }
  102.  
  103.  
  104.  
  105. void gl_DepthMask( GLcontext* ctx, GLboolean flag )
  106. {
  107.    if (INSIDE_BEGIN_END(ctx)) {
  108.       gl_error( ctx, GL_INVALID_OPERATION, "glDepthMask" );
  109.       return;
  110.    }
  111.  
  112.    /*
  113.     * GL_TRUE indicates depth buffer writing is enabled (default)
  114.     * GL_FALSE indicates depth buffer writing is disabled
  115.     */
  116.    ctx->Depth.Mask = flag;
  117.    ctx->NewState |= NEW_RASTER_OPS;
  118. }
  119.  
  120.  
  121.  
  122. /**********************************************************************/
  123. /*****                   Depth Testing Functions                  *****/
  124. /**********************************************************************/
  125.  
  126.  
  127. /*
  128.  * Depth test horizontal spans of fragments.  These functions are called
  129.  * via ctx->Driver.depth_test_span only.
  130.  *
  131.  * Input:  n - number of pixels in the span
  132.  *         x, y - location of leftmost pixel in span in window coords
  133.  *         z - array [n] of integer depth values
  134.  * In/Out:  mask - array [n] of flags (1=draw pixel, 0=don't draw) 
  135.  * Return:  number of pixels which passed depth test
  136.  */
  137.  
  138.  
  139. /*
  140.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  141.  */
  142. GLuint gl_depth_test_span_generic( GLcontext* ctx,
  143.                                    GLuint n, GLint x, GLint y,
  144.                                    const GLdepth z[],
  145.                                    GLubyte mask[] )
  146. {
  147.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  148.    GLubyte *m = mask;
  149.    GLuint i;
  150.    GLuint passed = 0;
  151.  
  152.    /* switch cases ordered from most frequent to less frequent */
  153.    switch (ctx->Depth.Func) {
  154.       case GL_LESS:
  155.          if (ctx->Depth.Mask) {
  156.         /* Update Z buffer */
  157.         for (i=0; i<n; i++,zptr++,m++) {
  158.            if (*m) {
  159.           if (z[i] < *zptr) {
  160.              /* pass */
  161.              *zptr = z[i];
  162.              passed++;
  163.           }
  164.           else {
  165.              /* fail */
  166.              *m = 0;
  167.           }
  168.            }
  169.         }
  170.      }
  171.      else {
  172.         /* Don't update Z buffer */
  173.         for (i=0; i<n; i++,zptr++,m++) {
  174.            if (*m) {
  175.           if (z[i] < *zptr) {
  176.              /* pass */
  177.              passed++;
  178.           }
  179.           else {
  180.              *m = 0;
  181.           }
  182.            }
  183.         }
  184.      }
  185.      break;
  186.       case GL_LEQUAL:
  187.      if (ctx->Depth.Mask) {
  188.         /* Update Z buffer */
  189.         for (i=0;i<n;i++,zptr++,m++) {
  190.            if (*m) {
  191.           if (z[i] <= *zptr) {
  192.              *zptr = z[i];
  193.              passed++;
  194.           }
  195.           else {
  196.              *m = 0;
  197.           }
  198.            }
  199.         }
  200.      }
  201.      else {
  202.         /* Don't update Z buffer */
  203.         for (i=0;i<n;i++,zptr++,m++) {
  204.            if (*m) {
  205.           if (z[i] <= *zptr) {
  206.              /* pass */
  207.              passed++;
  208.           }
  209.           else {
  210.              *m = 0;
  211.           }
  212.            }
  213.         }
  214.      }
  215.      break;
  216.       case GL_GEQUAL:
  217.      if (ctx->Depth.Mask) {
  218.         /* Update Z buffer */
  219.         for (i=0;i<n;i++,zptr++,m++) {
  220.            if (*m) {
  221.           if (z[i] >= *zptr) {
  222.              *zptr = z[i];
  223.              passed++;
  224.           }
  225.           else {
  226.              *m = 0;
  227.           }
  228.            }
  229.         }
  230.      }
  231.      else {
  232.         /* Don't update Z buffer */
  233.         for (i=0;i<n;i++,zptr++,m++) {
  234.            if (*m) {
  235.           if (z[i] >= *zptr) {
  236.              /* pass */
  237.              passed++;
  238.           }
  239.           else {
  240.              *m = 0;
  241.           }
  242.            }
  243.         }
  244.      }
  245.      break;
  246.       case GL_GREATER:
  247.      if (ctx->Depth.Mask) {
  248.         /* Update Z buffer */
  249.         for (i=0;i<n;i++,zptr++,m++) {
  250.            if (*m) {
  251.           if (z[i] > *zptr) {
  252.              *zptr = z[i];
  253.              passed++;
  254.           }
  255.           else {
  256.              *m = 0;
  257.           }
  258.            }
  259.         }
  260.      }
  261.      else {
  262.         /* Don't update Z buffer */
  263.         for (i=0;i<n;i++,zptr++,m++) {
  264.            if (*m) {
  265.           if (z[i] > *zptr) {
  266.              /* pass */
  267.              passed++;
  268.           }
  269.           else {
  270.              *m = 0;
  271.           }
  272.            }
  273.         }
  274.      }
  275.      break;
  276.       case GL_NOTEQUAL:
  277.      if (ctx->Depth.Mask) {
  278.         /* Update Z buffer */
  279.         for (i=0;i<n;i++,zptr++,m++) {
  280.            if (*m) {
  281.           if (z[i] != *zptr) {
  282.              *zptr = z[i];
  283.              passed++;
  284.           }
  285.           else {
  286.              *m = 0;
  287.           }
  288.            }
  289.         }
  290.      }
  291.      else {
  292.         /* Don't update Z buffer */
  293.         for (i=0;i<n;i++,zptr++,m++) {
  294.            if (*m) {
  295.           if (z[i] != *zptr) {
  296.              /* pass */
  297.              passed++;
  298.           }
  299.           else {
  300.              *m = 0;
  301.           }
  302.            }
  303.         }
  304.      }
  305.      break;
  306.       case GL_EQUAL:
  307.      if (ctx->Depth.Mask) {
  308.         /* Update Z buffer */
  309.         for (i=0;i<n;i++,zptr++,m++) {
  310.            if (*m) {
  311.           if (z[i] == *zptr) {
  312.              *zptr = z[i];
  313.              passed++;
  314.           }
  315.           else {
  316.              *m =0;
  317.           }
  318.            }
  319.         }
  320.      }
  321.      else {
  322.         /* Don't update Z buffer */
  323.         for (i=0;i<n;i++,zptr++,m++) {
  324.            if (*m) {
  325.           if (z[i] == *zptr) {
  326.              /* pass */
  327.              passed++;
  328.           }
  329.           else {
  330.              *m =0;
  331.           }
  332.            }
  333.         }
  334.      }
  335.      break;
  336.       case GL_ALWAYS:
  337.      if (ctx->Depth.Mask) {
  338.         /* Update Z buffer */
  339.         for (i=0;i<n;i++,zptr++,m++) {
  340.            if (*m) {
  341.           *zptr = z[i];
  342.           passed++;
  343.            }
  344.         }
  345.      }
  346.      else {
  347.         /* Don't update Z buffer or mask */
  348.         passed = n;
  349.      }
  350.      break;
  351.       case GL_NEVER:
  352.      for (i=0;i<n;i++) {
  353.         mask[i] = 0;
  354.      }
  355.      break;
  356.       default:
  357.          gl_problem(ctx, "Bad depth func in gl_depth_test_span_generic");
  358.    } /*switch*/
  359.  
  360.    return passed;
  361. }
  362.  
  363.  
  364.  
  365. /*
  366.  * glDepthFunc(GL_LESS) and glDepthMask(GL_TRUE).
  367.  */
  368. GLuint gl_depth_test_span_less( GLcontext* ctx,
  369.                                 GLuint n, GLint x, GLint y, const GLdepth z[],
  370.                                 GLubyte mask[] )
  371. {
  372.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  373.    GLuint i;
  374.    GLuint passed = 0;
  375.  
  376.    for (i=0; i<n; i++) {
  377.       if (mask[i]) {
  378.          if (z[i] < zptr[i]) {
  379.             /* pass */
  380.             zptr[i] = z[i];
  381.             passed++;
  382.          }
  383.          else {
  384.             /* fail */
  385.             mask[i] = 0;
  386.          }
  387.       }
  388.    }
  389.    return passed;
  390. }
  391.  
  392.  
  393. /*
  394.  * glDepthFunc(GL_GREATER) and glDepthMask(GL_TRUE).
  395.  */
  396. GLuint gl_depth_test_span_greater( GLcontext* ctx,
  397.                                    GLuint n, GLint x, GLint y,
  398.                                    const GLdepth z[],
  399.                                    GLubyte mask[] )
  400. {
  401.    GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  402.    GLuint i;
  403.    GLuint passed = 0;
  404.  
  405.    for (i=0; i<n; i++) {
  406.       if (mask[i]) {
  407.          if (z[i] > zptr[i]) {
  408.             /* pass */
  409.             zptr[i] = z[i];
  410.             passed++;
  411.          }
  412.          else {
  413.             /* fail */
  414.             mask[i] = 0;
  415.          }
  416.       }
  417.    }
  418.    return passed;
  419. }
  420.  
  421.  
  422.  
  423. /*
  424.  * Depth test an array of randomly positioned fragments.
  425.  */
  426.  
  427.  
  428. #define ZADDR_SETUP   GLdepth *depthbuffer = ctx->Buffer->Depth; \
  429.                       GLint width = ctx->Buffer->Width;
  430.  
  431. #define ZADDR( X, Y )   (depthbuffer + (Y) * width + (X) )
  432.  
  433.  
  434.  
  435. /*
  436.  * glDepthFunc( any ) and glDepthMask( GL_TRUE or GL_FALSE ).
  437.  */
  438. void gl_depth_test_pixels_generic( GLcontext* ctx,
  439.                                    GLuint n, const GLint x[], const GLint y[],
  440.                                    const GLdepth z[], GLubyte mask[] )
  441. {
  442.    register GLdepth *zptr;
  443.    register GLuint i;
  444.  
  445.    /* switch cases ordered from most frequent to less frequent */
  446.    switch (ctx->Depth.Func) {
  447.       case GL_LESS:
  448.          if (ctx->Depth.Mask) {
  449.         /* Update Z buffer */
  450.         for (i=0; i<n; i++) {
  451.            if (mask[i]) {
  452.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  453.           if (z[i] < *zptr) {
  454.              /* pass */
  455.              *zptr = z[i];
  456.           }
  457.           else {
  458.              /* fail */
  459.              mask[i] = 0;
  460.           }
  461.            }
  462.         }
  463.      }
  464.      else {
  465.         /* Don't update Z buffer */
  466.         for (i=0; i<n; i++) {
  467.            if (mask[i]) {
  468.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  469.           if (z[i] < *zptr) {
  470.              /* pass */
  471.           }
  472.           else {
  473.              /* fail */
  474.              mask[i] = 0;
  475.           }
  476.            }
  477.         }
  478.      }
  479.      break;
  480.       case GL_LEQUAL:
  481.          if (ctx->Depth.Mask) {
  482.         /* Update Z buffer */
  483.         for (i=0; i<n; i++) {
  484.            if (mask[i]) {
  485.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  486.           if (z[i] <= *zptr) {
  487.              /* pass */
  488.              *zptr = z[i];
  489.           }
  490.           else {
  491.              /* fail */
  492.              mask[i] = 0;
  493.           }
  494.            }
  495.         }
  496.      }
  497.      else {
  498.         /* Don't update Z buffer */
  499.         for (i=0; i<n; i++) {
  500.            if (mask[i]) {
  501.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  502.           if (z[i] <= *zptr) {
  503.              /* pass */
  504.           }
  505.           else {
  506.              /* fail */
  507.              mask[i] = 0;
  508.           }
  509.            }
  510.         }
  511.      }
  512.      break;
  513.       case GL_GEQUAL:
  514.          if (ctx->Depth.Mask) {
  515.         /* Update Z buffer */
  516.         for (i=0; i<n; i++) {
  517.            if (mask[i]) {
  518.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  519.           if (z[i] >= *zptr) {
  520.              /* pass */
  521.              *zptr = z[i];
  522.           }
  523.           else {
  524.              /* fail */
  525.              mask[i] = 0;
  526.           }
  527.            }
  528.         }
  529.      }
  530.      else {
  531.         /* Don't update Z buffer */
  532.         for (i=0; i<n; i++) {
  533.            if (mask[i]) {
  534.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  535.           if (z[i] >= *zptr) {
  536.              /* pass */
  537.           }
  538.           else {
  539.              /* fail */
  540.              mask[i] = 0;
  541.           }
  542.            }
  543.         }
  544.      }
  545.      break;
  546.       case GL_GREATER:
  547.          if (ctx->Depth.Mask) {
  548.         /* Update Z buffer */
  549.         for (i=0; i<n; i++) {
  550.            if (mask[i]) {
  551.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  552.           if (z[i] > *zptr) {
  553.              /* pass */
  554.              *zptr = z[i];
  555.           }
  556.           else {
  557.              /* fail */
  558.              mask[i] = 0;
  559.           }
  560.            }
  561.         }
  562.      }
  563.      else {
  564.         /* Don't update Z buffer */
  565.         for (i=0; i<n; i++) {
  566.            if (mask[i]) {
  567.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  568.           if (z[i] > *zptr) {
  569.              /* pass */
  570.           }
  571.           else {
  572.              /* fail */
  573.              mask[i] = 0;
  574.           }
  575.            }
  576.         }
  577.      }
  578.      break;
  579.       case GL_NOTEQUAL:
  580.          if (ctx->Depth.Mask) {
  581.         /* Update Z buffer */
  582.         for (i=0; i<n; i++) {
  583.            if (mask[i]) {
  584.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  585.           if (z[i] != *zptr) {
  586.              /* pass */
  587.              *zptr = z[i];
  588.           }
  589.           else {
  590.              /* fail */
  591.              mask[i] = 0;
  592.           }
  593.            }
  594.         }
  595.      }
  596.      else {
  597.         /* Don't update Z buffer */
  598.         for (i=0; i<n; i++) {
  599.            if (mask[i]) {
  600.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  601.           if (z[i] != *zptr) {
  602.              /* pass */
  603.           }
  604.           else {
  605.              /* fail */
  606.              mask[i] = 0;
  607.           }
  608.            }
  609.         }
  610.      }
  611.      break;
  612.       case GL_EQUAL:
  613.          if (ctx->Depth.Mask) {
  614.         /* Update Z buffer */
  615.         for (i=0; i<n; i++) {
  616.            if (mask[i]) {
  617.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  618.           if (z[i] == *zptr) {
  619.              /* pass */
  620.              *zptr = z[i];
  621.           }
  622.           else {
  623.              /* fail */
  624.              mask[i] = 0;
  625.           }
  626.            }
  627.         }
  628.      }
  629.      else {
  630.         /* Don't update Z buffer */
  631.         for (i=0; i<n; i++) {
  632.            if (mask[i]) {
  633.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  634.           if (z[i] == *zptr) {
  635.              /* pass */
  636.           }
  637.           else {
  638.              /* fail */
  639.              mask[i] = 0;
  640.           }
  641.            }
  642.         }
  643.      }
  644.      break;
  645.       case GL_ALWAYS:
  646.      if (ctx->Depth.Mask) {
  647.         /* Update Z buffer */
  648.         for (i=0; i<n; i++) {
  649.            if (mask[i]) {
  650.           zptr = Z_ADDRESS(ctx,x[i],y[i]);
  651.           *zptr = z[i];
  652.            }
  653.         }
  654.      }
  655.      else {
  656.         /* Don't update Z buffer or mask */
  657.      }
  658.      break;
  659.       case GL_NEVER:
  660.      /* depth test never passes */
  661.      for (i=0;i<n;i++) {
  662.         mask[i] = 0;
  663.      }
  664.      break;
  665.       default:
  666.          gl_problem(ctx, "Bad depth func in gl_depth_test_pixels_generic");
  667.    } /*switch*/
  668. }
  669.  
  670.  
  671.  
  672. /*
  673.  * glDepthFunc( GL_LESS ) and glDepthMask( GL_TRUE ).
  674.  */
  675. void gl_depth_test_pixels_less( GLcontext* ctx,
  676.                                 GLuint n, const GLint x[], const GLint y[],
  677.                                 const GLdepth z[], GLubyte mask[] )
  678. {
  679.    GLdepth *zptr;
  680.    GLuint i;
  681.  
  682.    for (i=0; i<n; i++) {
  683.       if (mask[i]) {
  684.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  685.          if (z[i] < *zptr) {
  686.             /* pass */
  687.             *zptr = z[i];
  688.          }
  689.          else {
  690.             /* fail */
  691.             mask[i] = 0;
  692.          }
  693.       }
  694.    }
  695. }
  696.  
  697.  
  698. /*
  699.  * glDepthFunc( GL_GREATER ) and glDepthMask( GL_TRUE ).
  700.  */
  701. void gl_depth_test_pixels_greater( GLcontext* ctx,
  702.                                    GLuint n, const GLint x[], const GLint y[],
  703.                                    const GLdepth z[], GLubyte mask[] )
  704. {
  705.    GLdepth *zptr;
  706.    GLuint i;
  707.  
  708.    for (i=0; i<n; i++) {
  709.       if (mask[i]) {
  710.          zptr = Z_ADDRESS(ctx,x[i],y[i]);
  711.          if (z[i] > *zptr) {
  712.             /* pass */
  713.             *zptr = z[i];
  714.          }
  715.          else {
  716.             /* fail */
  717.             mask[i] = 0;
  718.          }
  719.       }
  720.    }
  721. }
  722.  
  723.  
  724.  
  725.  
  726. /**********************************************************************/
  727. /*****                      Read Depth Buffer                     *****/
  728. /**********************************************************************/
  729.  
  730.  
  731. /*
  732.  * Return a span of depth values from the depth buffer as floats in [0,1].
  733.  * This function is only called through Driver.read_depth_span_float()
  734.  * Input:  n - how many pixels
  735.  *         x,y - location of first pixel
  736.  * Output:  depth - the array of depth values
  737.  */
  738. void gl_read_depth_span_float( GLcontext* ctx,
  739.                                GLuint n, GLint x, GLint y, GLfloat depth[] )
  740. {
  741.    GLdepth *zptr;
  742.    GLfloat scale;
  743.    GLuint i;
  744.  
  745.    scale = 1.0F / DEPTH_SCALE;
  746.  
  747.    if (ctx->Buffer->Depth) {
  748.       zptr = Z_ADDRESS( ctx, x, y );
  749.       for (i=0;i<n;i++) {
  750.      depth[i] = (GLfloat) zptr[i] * scale;
  751.       }
  752.    }
  753.    else {
  754.       for (i=0;i<n;i++) {
  755.      depth[i] = 0.0F;
  756.       }
  757.    }
  758. }
  759.  
  760.  
  761. /*
  762.  * Return a span of depth values from the depth buffer as integers in
  763.  * [0,MAX_DEPTH].
  764.  * This function is only called through Driver.read_depth_span_int()
  765.  * Input:  n - how many pixels
  766.  *         x,y - location of first pixel
  767.  * Output:  depth - the array of depth values
  768.  */
  769. void gl_read_depth_span_int( GLcontext* ctx,
  770.                              GLuint n, GLint x, GLint y, GLdepth depth[] )
  771. {
  772.    if (ctx->Buffer->Depth) {
  773.       GLdepth *zptr = Z_ADDRESS( ctx, x, y );
  774.       MEMCPY( depth, zptr, n * sizeof(GLdepth) );
  775.    }
  776.    else {
  777.       GLuint i;
  778.       for (i=0;i<n;i++) {
  779.      depth[i] = 0;
  780.       }
  781.    }
  782. }
  783.  
  784.  
  785.  
  786. /**********************************************************************/
  787. /*****                Allocate and Clear Depth Buffer             *****/
  788. /**********************************************************************/
  789.  
  790.  
  791.  
  792. /*
  793.  * Allocate a new depth buffer.  If there's already a depth buffer allocated
  794.  * it will be free()'d.  The new depth buffer will be uniniitalized.
  795.  * This function is only called through Driver.alloc_depth_buffer.
  796.  */
  797. void gl_alloc_depth_buffer( GLcontext* ctx )
  798. {
  799.    /* deallocate current depth buffer if present */
  800.    if (ctx->Buffer->Depth) {
  801.       free(ctx->Buffer->Depth);
  802.       ctx->Buffer->Depth = NULL;
  803.    }
  804.  
  805.    /* allocate new depth buffer, but don't initialize it */
  806.    ctx->Buffer->Depth = (GLdepth *) malloc( ctx->Buffer->Width
  807.                                             * ctx->Buffer->Height
  808.                                             * sizeof(GLdepth) );
  809.    if (!ctx->Buffer->Depth) {
  810.       /* out of memory */
  811.       ctx->Depth.Test = GL_FALSE;
  812.       gl_error( ctx, GL_OUT_OF_MEMORY, "Couldn't allocate depth buffer" );
  813.    }
  814. }
  815.  
  816.  
  817.  
  818.  
  819. /*
  820.  * Clear the depth buffer.  If the depth buffer doesn't exist yet we'll
  821.  * allocate it now.
  822.  * This function is only called through Driver.clear_depth_buffer.
  823.  */
  824. void gl_clear_depth_buffer( GLcontext* ctx )
  825. {
  826.    GLdepth clear_value = (GLdepth) (ctx->Depth.Clear * DEPTH_SCALE);
  827.  
  828.    if (ctx->Visual->DepthBits==0 || !ctx->Buffer->Depth || !ctx->Depth.Mask) {
  829.       /* no depth buffer, or writing to it is disabled */
  830.       return;
  831.    }
  832.  
  833.    /* The loops in this function have been written so the IRIX 5.3
  834.     * C compiler can unroll them.  Hopefully other compilers can too!
  835.     */
  836.  
  837.    if (ctx->Scissor.Enabled) {
  838.       /* only clear scissor region */
  839.       GLint y;
  840.       for (y=ctx->Buffer->Ymin; y<=ctx->Buffer->Ymax; y++) {
  841.          GLdepth *d = Z_ADDRESS( ctx, ctx->Buffer->Xmin, y );
  842.          GLint n = ctx->Buffer->Xmax - ctx->Buffer->Xmin + 1;
  843.          do {
  844.             *d++ = clear_value;
  845.             n--;
  846.          } while (n);
  847.       }
  848.    }
  849.    else {
  850.       /* clear whole buffer */
  851.       if (sizeof(GLdepth)==2 && (clear_value&0xff)==(clear_value>>8)) {
  852.          /* lower and upper bytes of clear_value are same, use MEMSET */
  853.          MEMSET( ctx->Buffer->Depth, clear_value&0xff,
  854.                  2*ctx->Buffer->Width*ctx->Buffer->Height);
  855.       }
  856.       else {
  857.          GLdepth *d = ctx->Buffer->Depth;
  858.          GLint n = ctx->Buffer->Width * ctx->Buffer->Height;
  859.          while (n>=16) {
  860.             d[0] = clear_value;    d[1] = clear_value;
  861.             d[2] = clear_value;    d[3] = clear_value;
  862.             d[4] = clear_value;    d[5] = clear_value;
  863.             d[6] = clear_value;    d[7] = clear_value;
  864.             d[8] = clear_value;    d[9] = clear_value;
  865.             d[10] = clear_value;   d[11] = clear_value;
  866.             d[12] = clear_value;   d[13] = clear_value;
  867.             d[14] = clear_value;   d[15] = clear_value;
  868.             d += 16;
  869.             n -= 16;
  870.          }
  871.          while (n>0) {
  872.             *d++ = clear_value;
  873.             n--;
  874.          }
  875.       }
  876.    }
  877. }
  878.  
  879.  
  880.  
  881.